package com.eig.courseweb.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.eig.common.entity.course.CheckCourse;
import com.eig.common.entity.course.EduCourse;
import com.eig.common.vo.Result;
import com.eig.courseweb.service.CourseWebService;
import com.eig.courseweb.utils.AliOSSClientUtil;
import com.eig.courseweb.utils.JsonResult;
import com.eig.courseweb.utils.ossFileUtil;
import com.eig.courseweb.vo.CourseVo;
import com.eig.courseweb.vo.ShouCangVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Random;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2022/12/30 上午 11:53
 * @description：
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("core/course")
@Api(tags = "前段课程列表")
@EnableSwagger2
@Log
public class CourseWebController {

    @Resource
    private CourseWebService courseWebService;

    @PostMapping("/getAllMenu")
    @ApiOperation(value = "课程查询查询")
    public Result getAllMenu() {
        return courseWebService.getAllMenu();
    }


    @PostMapping("/fontFindCourse/{pageCurrent}/{pageSize}")
    @ApiOperation(value = "分页查询", notes = "根据current和pageSize查询")
    public Result fontFindCourse(@PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @RequestBody CourseVo e) {
        PageRequest pageRequest = PageRequest.of(pageCurrent - 1, pageSize);
        return courseWebService.fontFindCourse(e, pageRequest);
    }

    @PostMapping("/queryAllCourseAndTeacherName/{id}")
    @ApiOperation(value = "根据课程id查询学生和教师信息")
    public Result queryAllCourseAndTeacherName(@PathVariable String id) {
        return courseWebService.queryAllCourseAndTeacherName(id);
    }
    @PostMapping("/queryAllById/{id}/{mobileId}")
    @ApiOperation(value = "根据手机号查询该视频信息是否该讲师发布的")
    public Result queryAllById(@PathVariable String id,@PathVariable String mobileId) {
        return courseWebService.queryAllById(id,mobileId);
    }
    @PostMapping("/queryMemberStatus/{id}/{mobileId}")
    @ApiOperation(value = "根据课程id和会员id判断当前账号是否为讲师")
    public Result queryMemberStatus(@PathVariable String id,@PathVariable String mobileId) {
        return courseWebService.queryMemberStatus(id,mobileId);
    }
    @GetMapping("/findTeacherByTeacherId/{teacherId}")
    @ApiOperation(value = "获取讲师课程信息", notes = "根据teacherId查询")
    public Result findTeacherByTeacherId(@PathVariable String teacherId) {
        return courseWebService.findTeacherByTeacherId(teacherId);
    }

    @PostMapping("/findCourseByTeacherId/{pageCurrent}/{pageSize}")
    @ApiOperation(value = "分页查询", notes = "根据current和pageSize查询")
    public Result findCourseByTeacherId(@PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @RequestBody ShouCangVo e) {
        PageRequest pageRequest = PageRequest.of(pageCurrent - 1, pageSize);
        return courseWebService.findCourseByTeacherId(e, pageRequest);
    }

    @PostMapping("/changeStatus")
    @ApiOperation(value = "上下架操作")
    public Result changeStatus(@RequestBody EduCourse e) {
        return courseWebService.changeStatus(e);
    }

    @GetMapping("/getCoursByTeacherPhone/{pageCurrent}/{pageSize}/{teacherMobile}/{checkStatus}")
    @ApiOperation(value = "讲师录播管理", notes = "根据current和pageSize查询")
    public Result getCoursByTeacherPhone(@PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable String teacherMobile, @PathVariable String checkStatus) {
        PageRequest pageRequest = PageRequest.of(pageCurrent - 1, pageSize);
        return courseWebService.getCoursByTeacherPhone(pageRequest, teacherMobile, checkStatus);
    }

    @PostMapping("/updateCourseSubject")
    @ApiOperation(value = "修改操作")
    public Result updateCourseSubject(@RequestBody EduCourse e) {
        if (ObjectUtil.isNotEmpty(e.getId())) {
            return courseWebService.updateCourseSubject(e);
        } else {
            return courseWebService.InsterCourseSubject(e);
        }
    }

    @PostMapping("/uploadMsg")
    @ApiOperation(value = "oss图片上传操作")
    public String uploadMsg(MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                // 生成文件名称
                String nameSuffix = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."))
                        .replaceAll(" ", "_").replaceAll(",", "") + format.format(DateUtil.date())
                        + new Random().nextInt(1000);
                //上传原始图片到阿里云
                String uploadPath = AliOSSClientUtil.uploadVideo(file);
                JsonResult jsonResult = new JsonResult(true, "上传成功", uploadPath);
                return uploadPath;
            } catch (Exception e) {
                log.info("上传附件错误" + e.getMessage());
//                return new JsonResult(false, "系统未知错误");
            }
        } else {
//            return new JsonResult(false, "文件不能为空");
        }
        return null;
    }

}
